package com.phone.client.service.impl;

import com.phone.client.mapper.*;
import com.phone.client.poji.entry.*;
import com.phone.client.service.OrderService;
import com.phone.client.utils.OSSUtil;
import com.phone.client.utils.OrcScanUtil;
import com.phone.client.utils.car.OssUtil;
import com.seventh.common.pojo.R;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @program: clms-seventh
 * @description: 订单业务逻辑层
 * @author: 张静
 * @create: 2021-08-07 11:27
 **/
@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private ClientMapper clientMapper;
    @Resource
    private InterMapper interMapper;
    @Resource
    private CarMapper carMapper;
    @Resource
    private CarTypeMapper typeMapper;
    @Override
    @Transactional
    public R insertOrder(TbOrder order) {
        TbClient client = clientMapper.selectById(order.getClientId());
        Integer storyIntegration = client.getIntegration();
        client.setHistoryIntegration(storyIntegration.toString());
        Integer count=0;
        if (order.getTotalPrice()>(double) client.getIntegration()){
            Double TotalPrice = order.getTotalPrice() - (double) client.getIntegration();
            order.setTotalPrice(TotalPrice);
            client.setIntegration(0);
            count=client.getIntegration();
            if (order.getPayType().equals("余额")){
                Double nowac = client.getAccountBalance() - order.getTotalPrice();
                client.setAccountBalance(nowac);
            }
        }else {
            Double integration = (double) client.getIntegration() - order.getTotalPrice();
            client.setIntegration(integration.intValue());
            count=(int) order.getTotalPrice();
            order.setTotalPrice(0);

        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        /* System.out.println(df.format(new Date()));// new Date()为获取当前系统时间*/
        Long startTs = System.currentTimeMillis();
        String orderNo = order.getClientId() + String.valueOf(startTs);//生成订单便编号用于生成二维码
        order.setOrderNo(orderNo);
        order.setAddtime(new Date());
        clientMapper.updateById(client);
        orderMapper.insert(order);
        TbIntegrationHistory history=new TbIntegrationHistory();
        history.setMemberId((int)order.getClientId());
        history.setCreateTime(df.format(new Date()));
        history.setChangeType(2);
        history.setChangeCount(count);
        history.setOperateMan(client.getName());
        history.setOperateNote("消费积分");
        history.setSourceType(0);
        interMapper.insert(history);
        return R.ok().put("orderNo",order.getOrderNo());
    }

    @Override
    public R supplementaryInfo(MultipartFile file, String type) {
        try {
            String imgurl = OssUtil.upload(file.getInputStream(), file.getOriginalFilename());
            Map map = OrcScanUtil.IdInfo(imgurl, type);
            if (map != null) {
                map.put("imgurl", imgurl);
                return R.ok().put("userinfo", map);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.error().message("信息不识别");
    }

    @Override
    public R addInfo(Map map) {
        TbClient tbClient = clientMapper.selectById((Integer) map.get("id"));
        Map faceinfo = (Map) map.get("faceinfo");
        Map backinfo = (Map) map.get("backinfo");
        tbClient.setSex(faceinfo.get("sex").toString());
        tbClient.setAddress(faceinfo.get("address").toString());
        tbClient.setLicenseNo(faceinfo.get("num").toString());
        tbClient.setName(faceinfo.get("name").toString());
        tbClient.setPathFront(faceinfo.get("imgurl").toString());
        tbClient.setPathReverse(backinfo.get("imgurl").toString());
        clientMapper.updateById(tbClient);
        return R.ok().message("已完成信息完善").put("userInfo",tbClient);
    }

    @Override
    public R getOrderCar(Integer carId) {
        TbCar tbCar = carMapper.selectById(carId);
        TbCarType tbCarType = typeMapper.selectById(tbCar.getType());
        tbCar.setTypeName(tbCarType.getTypeName());
        return R.ok().put("tbCar",tbCar);
    }
}
